from dao.nurse import ratings_time_sum_dao
from datetime import datetime, timedelta

def item_time():

    sql_query = start_date()

    result = ratings_time_sum_dao.item_time(sql_query)

    if result:
        result = {"code": 20000, "err": 0, "message": "查询成功",
                  "data": {"userinfo": result}}
    else:
        result = {"err": 2, "message": "无数据", "data": {}}
    return result

def start_date():

    # 获取最原始的日期
    start_date = ratings_time_sum_dao.get_original_date()

    # 获取今天的日期
    end_date = datetime.today()

    # 生成日期列表
    date_list = []
    current_date = start_date

    while current_date <= end_date:
        date_list.append(current_date.strftime('%Y-%m-%d'))
        current_date += timedelta(days=1)

    # 构建 SQL 查询
    sql_query = "SELECT '" + date_list[0] + "' AS rating_date"
    for date in date_list[1:]:
        sql_query += f" UNION ALL SELECT '{date}' AS rating_date"

    return sql_query

def time_month_sum():

    result = ratings_time_sum_dao.time_month_sum()

    if result:
        result = {"code": 20000, "err": 0, "message": "查询成功",
                  "data": {"userinfo": result}}
    else:
        result = {"err": 2, "message": "无数据", "data": {}}
    return result


if __name__ == '__main__':
    ret = item_time()
    print(ret)
